package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.admin.goods.GoodsDetailVO;
import com.cskaoyan.bean.vo.admin.topic.ShowTopicInfo;
import com.cskaoyan.service.admin.GoodsService;
import com.cskaoyan.service.admin.TopicService;
import com.github.pagehelper.PageInfo;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author by yxy_8175@163.com
 * @Description TODO
 * @Date 2022-11-22 23:36
 */

@RestController
@RequestMapping("/wx/topic")
public class WxTopicController {
    private final Log logger = LogFactory.getLog(WxTopicController.class);

    @Autowired
    private TopicService topicService;

    @Autowired
    private GoodsService goodsService;


    /*
     * @Description TODO 专题列表
     * @param [pageInfo]
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @date 2022-11-22 23:54
     * @auther yxy_8175@163.com
     */
    @GetMapping("list")
    public BaseRespVo list(BasePageInfo pageInfo) {
        CommonData<MarketTopic> topicList = topicService.selectByPage(pageInfo, null, null);
        return BaseRespVo.ok(topicList);
    }

    /*
     * 专题详情
     * @Description  查询专题文章详细信息
     * @param id 专题id
     * @return 
     * @date 2022-11-22 23:56
     * @auther yxy_8175@163.com
    */
    @GetMapping("detail")
    public BaseRespVo detail(@NotNull Integer id){
        ShowTopicInfo showTopicInfo = topicService.selectTopicById(id);

        // 存放商品的容器
        List<MarketGoods> goods = new ArrayList<>();
        // 获取专题中的存放的商品id
        String[] goodIds = showTopicInfo.getTopic().getGoods();
        for (String goodId : goodIds) {
            MarketGoods good = goodsService.queryGoodById(Integer.parseInt(goodId));
            if (null != good){
                goods.add(good);
            }
        }

        HashMap<String, Object> result = new HashMap<>();
        result.put("topic",showTopicInfo.getTopic());
        result.put("goods",goods);
        return BaseRespVo.ok(result);
    }

    /*
     * @Description TODO 相关专题
     * @param
     * @return 
     * @date 2022-11-23 0:27
     * @auther yxy_8175@163.com
    */
    @GetMapping("related")
    public BaseRespVo related(@NotNull Integer id){
        BasePageInfo pageInfo = new BasePageInfo();
        pageInfo.setPage(1);
        pageInfo.setLimit(4);
        pageInfo.setSort("add_time");
        pageInfo.setOrder("desc");
        CommonData<MarketTopic> data = topicService.queryRelatedList(id,pageInfo);
        return BaseRespVo.ok(data);
    }

}
